<?php
/**
 * 产品供应商控制器
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/12/21
 * Time: 16:18
 */

namespace Home\Controller;


use Home\Services\ProductSupplierManageService;

class ProductSupplierManageController extends CommunalController
{
    //产品供应商首页
    public function index()
    {
        $where['del'] = 1;
        $res = ProductSupplierManageService::getResultArray('*', $where, 'id desc');
        $types = array_unique(array_column($res, 'type'));
        $keyword = I("keyword");
        if (!empty($keyword)) $where['name|file_name'] = array('like', "%$keyword%");
        $type = I("type");
        if (!empty($type)) $where['type'] = $type;
        $province_id = I("province_id");
        if (!empty($province_id)) $where['province_id'] = $province_id;
        $label = I("label");
        if (!empty($label)) $where['_string'] = "FIND_IN_SET('$label', label)";
        $result = ProductSupplierManageService::getResultArray('*', $where, 'id desc');
        $area_ids = array_unique(array_column($result, 'area_id'));
        $where_area['id'] = array('in', $area_ids);
        $result_area = M("db_universal_label2")->where($where_area)->select();
        $res_area = M("db_universal_label2")->where("first_id=46 and id !=365")->select();
        foreach ($result as $key => $value) {
            foreach ($result_area as $area) {
                if ($value['area_id'] == $area['id']) {
                    $result[$key]['area_name'] = $area['second_name'];
                }
            }
        }
        $res_label = explode(',', implode(',', array_unique(array_filter(array_column($res, 'label')))));
        $province_ids = array_unique(array_column($res,'province_id'));
        $city_ids = array_unique(array_column($res,'city_id'));
        $province = self::get_province_city_all($province_ids);
        $city = self::get_province_city_all($city_ids);
        foreach ($result as $k =>$v){
            foreach ($province as $pro){
                if ($v['province_id'] == $pro['id']){
                    $result[$k]['pro_name'] = $pro['name'];
                }
            }
            foreach ($city as $c){
                if ($v['city_id'] == $c['id']){
                    $result[$k]['c_name'] = $c['name'];
                }
            }
        }
        $this->assign('res', $res);
        $this->assign('province', $province);
        $this->assign('types', $types);
        $this->assign('keyword', $keyword);
        $this->assign('type', $type);
        $this->assign('res_area', $res_area);
        $this->assign('province_id', $province_id);
        $this->assign('res_label', $res_label);
        $this->assign('label', $label);
        $count = count($result);
        $Page = new \Think\Page($count, 15);
        $result = array_slice($result, $Page->firstRow, $Page->listRows);
        $show = $Page->show();
        $this->assign('result', $result);
        $this->assign('page', $show);
        $this->display();

    }

    //产品供应商添加页
    public function add()
    {
        $resultProduct = ProductSupplierManageService::getProductResultOrder('*', null, 'id desc');
        $this->assign('result_product', $resultProduct);
        $this->assign('screen', $this->library_type_list(10, NULL, NULL, 1));
        $this->assign('province_list', $this->get_province_city());
        if (IS_POST == true) {
            $data = $_POST;
            $db = M();
            $db->startTrans();//开启事物
            if (!empty($data['new_type'])) {
                $data['type'] = $data['new_type'];
                unset($data['new_type']);
                ProductSupplierManageService::addProduct(array('type' => $_POST['new_type']));
            }
            $supplier_data = ProductSupplierManageService::addSupplier($data);
            if (!empty($supplier_data)) {
                //提交
                $db->commit();
                $this->success('添加成功', __APP__ . 'ProductSupplierManage/index');
                die;
            } else {
                //回滚
                $db->rollback();
                $this->error('添加失败', __APP__ . 'ProductSupplierManage/index');
                die;
            }
        }
        $this->display();
    }

    //删除
    public function del()
    {
        $id = I('id');
        $res = ProductSupplierManageService::delProduct($id);
        if (!empty($res)) {
            $this->success('删除成功', __APP__ . "ProductSupplierManage/index");
        } else {
            $this->error('删除失败', __APP__ . "ProductSupplierManage/index");
        }
    }

    //编辑
    public function edit()
    {
        $where['id'] = I('get.id');
        $result = ProductSupplierManageService::getProductResult($where);
        $this->assign('res_type', ProductSupplierManageService::getProductResultOrder('*', null, 'id desc'));
        $this->assign('result', $result);
        $this->assign('screen', $this->library_type_list(10, NULL, NULL, 1));
        $this->assign('province_list', $this->get_province_city());
        $this->assign('city_list', $this->get_province_city($result['province_id']));
        if (IS_POST == true) {
            $res = ProductSupplierManageService::edit($where, $_POST);
            if (!empty($res)) {
                $this->success('更新成功', __APP__ . "ProductSupplierManage/index");
                die;
            } else {
                $this->error('更新失败', __APP__ . "ProductSupplierManage/index");
                die;
            }
        }
        $this->display();
    }

    public static function get_province_city_all($ids)
    {
        $db_province_city = M('db_province_city');
        $where['id'] = array('in', $ids);
        $result = $db_province_city->where($where)->select();
        return $result;
    }

}